/*
 * MessageResource.cpp
 *
 *  Created on: 2013. 9. 10.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "MessageResource.h"
#include "../NUtil/Utils.h"

namespace ProjectZ { namespace NResource {

MessageResource::MessageResource()
{
	pMsgAD	= new MessageData;
	pMsgIOS	= new MessageData;
}

MessageResource::~MessageResource()
{
	delete pMsgAD;
	delete pMsgIOS;
}

bool
MessageResource::LoadResource()
{
	if (false == pMsgAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pMsgIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

MessageData *
MessageResource::GetMessageData(User * pUser)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pMsgAD;

	case NUtil::MarketType::IOS_KAKAO:
		return pMsgIOS;

	default:
		return NULL;
	}

	return NULL;
}

MessageData::Flyweight *
MessageResource::GetFly(int company, int salecode, int actionType)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(company, salecode);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pMsgAD->FindFly(actionType);

	case NUtil::MarketType::IOS_KAKAO:
		return pMsgIOS->FindFly(actionType);

	default:
		return NULL;
	}

	return NULL;
}

MessageData::~MessageData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
MessageData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool
MessageData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery(
			"SELECT NATIONAL_CODE, type, message0_title, message0, "
			"message1_title, message1, "
			"message2_title, message2, "
			"message3_title, message3, "
            "UNIX_TIMESTAMP(start_date1), UNIX_TIMESTAMP(end_date1), "
            "UNIX_TIMESTAMP(start_date2), UNIX_TIMESTAMP(end_date2), "
            "UNIX_TIMESTAMP(start_date3), UNIX_TIMESTAMP(end_date3) "
			"FROM resource_message "
			"WHERE company = %d AND sale_cd = %d",
			company, saleCode
			);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		MessageData::Flyweight * pFly = new MessageData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> pFly->_ACTION_TYPE
				>> pFly->_MSG0_TITLE >> pFly->_MSG0
				>> pFly->_MSG1_TITLE >> pFly->_MSG1
				>> pFly->_MSG2_TITLE >> pFly->_MSG2
				>> pFly->_MSG3_TITLE >> pFly->_MSG3
				>> pFly->_S_TIME1 >> pFly->_E_TIME1
				>> pFly->_S_TIME2 >> pFly->_E_TIME2
				>> pFly->_S_TIME3 >> pFly->_E_TIME3;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_ACTION_TYPE] = pFly;
	}

	return true;
}

MessageData::DATATREE &
MessageData::GetTree()
{
	return clsFlyTree;
}

MessageData::Flyweight *
MessageData::FindFly(int actionType)
{
	auto iter = clsFlyTree.find(actionType);

	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

bool
MessageData::IsKakaoMessage(int actionType)
{
	if (actionType >= static_cast<int>(message_type::_kakao_message_max))
		return false;

	return true;
}

int
MessageData::GetKakaoMessageCount()
{
	int msgCount = 0;

	auto iter = clsFlyTree.begin();

	for (; iter != clsFlyTree.end(); ++iter)
	{
		if (false == IsKakaoMessage(iter->first))
			continue;

		msgCount++;
	}

	return msgCount;
}

bool
MessageResource::SetMessageFromType(int company, int salecode, MessageData::message_type eType, std::string & sender, std::string & message)
{
	int actionType = static_cast<int>(eType);

	MessageData::Flyweight * pFly = GetFly(company, salecode, actionType);
	if (NULL == pFly)
	{
		return false;
	}

	time_t curTime = time(NULL);

	if ( (pFly->_S_TIME1 <= curTime && curTime <= pFly->_E_TIME1)
			|| (pFly->_S_TIME2 <= curTime && curTime <= pFly->_E_TIME2)
			|| (pFly->_S_TIME3 <= curTime && curTime <= pFly->_E_TIME3) )
	{
		int value = rand() % 3;

		switch (value)
		{
		case 0:
			sender	= pFly->_MSG1_TITLE.c_str();
			message	= pFly->_MSG1.c_str();
			break;
		case 1:
			sender	= pFly->_MSG2_TITLE.c_str();
			message	= pFly->_MSG2.c_str();
			break;
		case 2:
			sender	= pFly->_MSG3_TITLE.c_str();
			message	= pFly->_MSG3.c_str();
			break;
		}
	}
	else
	{
		sender	= pFly->_MSG0_TITLE.c_str();
		message	= pFly->_MSG0.c_str();
	}

	return true;
}

} /* NResource */ } /* ProjectZ */
